<?php
/*
 * litchicms
 * Copyright (C) 2010-5-1 Author: tiefa(539247@qq.com)
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

!defined('BRK') && exit("!");

/*
 * template_parse_foreach 解析
 * @param $string foreach字符串
 * @param $info_array 记录关系数组 防止递归嵌套
 * @return $string
*/
function template_parse_if($string) {

    $all_if=array();
    preg_match_all("/\{if\s([^\{]*?)\}/is", $string, $all_if);
    if(count($all_if[0])==0) return $string;
    $replacement=array();
    foreach ($all_if[1] as $k=>&$v) {
        $tempstring='';
        $tempstring.="<?php if(".$v."){ ?>";
        $replacement[]=$tempstring;
    }
    template_arr2patt($all_if[0]);
    $string=preg_replace($all_if[0], $replacement, $string, 1);

    $all_if=array();
    preg_match_all("/\{elseif\s([^\{]*?)\}/is", $string, $all_if);
    $replacement=array();
    foreach ($all_if[1] as $k=>&$v) {
        $tempstring='';
        $tempstring.="<?php }elseif(".$v."){ ?>";
        $replacement[]=$tempstring;
    }
    template_arr2patt($all_if[0]);
    $string=preg_replace($all_if[0], $replacement, $string, 1);

    $string=preg_replace("/\{\/if\}/", "<?php } ?>", $string);
    $string=preg_replace("/\{else\}/", "<?php }else{ ?>", $string);
    
    return $string;
}
?>